@echo off

set RUN_SCRIPT=%~dp0%run_script.cmd
set RUN_CORE=%~dp0%run_core.cmd
REM echo DB script: %~f0%
REM echo Run script: %RUN_SCRIPT%

SET DBNAME=%1
shift

IF "%DBSERVER%" == "" SET DBSERVER=LOCALHOST\SQLEXPRESS

echo Database: %DBNAME%
echo Server: %DBSERVER%

:again
if "%1" == "/zap" call %RUN_CORE% %DBNAME% 	/drop-db /create-db /all

if "%1" == "/all" call %RUN_CORE% %DBNAME% /table-p1 /table-p2 /relation /view-count /view-base /rel-sp-add /rel-sp-delete /rel-sp-update /rel-sp-count /sp-add /sp-delete /sp-update /sp-count /sp-search /sp-get /sp-getbyid

if "%1" == "/create-db" (
	echo Action: CREATE DATABASE
	osql -E -S %DBSERVER% -d master -Q "CREATE DATABASE [%DBNAME%]"
)

if "%1" == "/drop-db" (
	echo Action: DROP DATABASE
	osql -E -S %DBSERVER% -d master -Q "ALTER DATABASE [%DBNAME%] SET SINGLE_USER WITH ROLLBACK IMMEDIATE"
	osql -E -S %DBSERVER% -d master -Q "ALTER DATABASE [%DBNAME%] SET SINGLE_USER"
	osql -E -S %DBSERVER% -d master -Q "DROP DATABASE [%DBNAME%]"
)


if "%1" == "/clean" ( for %%i in (op\*.sql) do ( call %RUN_SCRIPT% %%i & if exist %%i.err exit /b 1 ) )
if "%1" == "/clean-views" ( for %%i in (op\drop_views.sql) do ( call %RUN_SCRIPT% %%i & if exist %%i.err exit /b 1 ) )
if "%1" == "/clean-tables" ( for %%i in (op\drop_tables.sql) do ( call %RUN_SCRIPT% %%i & if exist %%i.err exit /b 1 ) )
if "%1" == "/clean-sps" ( for %%i in (op\drop_storeprocs.sql) do ( call %RUN_SCRIPT% %%i & if exist %%i.err exit /b 1 ) )

if "%1" == "/table-p1" ( for %%i in (create_table_p1_*.sql) do ( call %RUN_SCRIPT% %%i & if exist %%i.err exit /b 1 ) )
if "%1" == "/table-p2" ( for %%i in (create_table_p2_*.sql) do ( call %RUN_SCRIPT% %%i & if exist %%i.err exit /b 1 ) )	
if "%1" == "/relation" ( for %%i in (create_relation_*.sql) do ( call %RUN_SCRIPT% %%i & if exist %%i.err exit /b 1 ) )
if "%1" == "/rel-sp-add" ( for %%i in (create_rel_sp_add_*.sql) do ( call %RUN_SCRIPT% %%i & if exist %%i.err exit /b 1 ) )
if "%1" == "/rel-sp-update" ( for %%i in (create_rel_sp_update_*.sql) do ( call %RUN_SCRIPT% %%i & if exist %%i.err exit /b 1 ) )
if "%1" == "/rel-sp-delete" ( for %%i in (create_rel_sp_delete_*.sql) do ( call %RUN_SCRIPT% %%i & if exist %%i.err exit /b 1 ) )
if "%1" == "/rel-sp-count" ( for %%i in (create_rel_sp_count_*.sql) do ( call %RUN_SCRIPT% %%i & if exist %%i.err exit /b 1 ) )
if "%1" == "/view-count" ( for %%i in (create_view_count_*.sql) do ( call %RUN_SCRIPT% %%i & if exist %%i.err exit /b 1 ) )
if "%1" == "/view-base" ( for %%i in (create_view_base_*.sql) do ( call %RUN_SCRIPT% %%i & if exist %%i.err exit /b 1 ) )
if "%1" == "/sp-add" ( for %%i in (create_sp_add_*.sql) do ( call %RUN_SCRIPT% %%i & if exist %%i.err exit /b 1 ) )
if "%1" == "/sp-delete" ( for %%i in (create_sp_delete_*.sql) do ( call %RUN_SCRIPT% %%i & if exist %%i.err exit /b 1 ) )
if "%1" == "/sp-update" ( for %%i in (create_sp_update_*.sql) do ( call %RUN_SCRIPT% %%i & if exist %%i.err exit /b 1 ) )
if "%1" == "/sp-count" ( for %%i in (create_sp_count_*.sql) do ( call %RUN_SCRIPT% %%i & if exist %%i.err exit /b 1 ) )
if "%1" == "/sp-search" ( for %%i in (create_sp_search_*.sql) do ( call %RUN_SCRIPT% %%i & if exist %%i.err exit /b 1 ) )
if "%1" == "/sp-get" ( for %%i in (create_sp_get_*.sql) do ( call %RUN_SCRIPT% %%i & if exist %%i.err exit /b 1 ) )
if "%1" == "/sp-getbyid" ( for %%i in (create_sp_getbyid_*.sql) do ( call %RUN_SCRIPT% %%i & if exist %%i.err exit /b 1 ) )
	

if "%1" == "/pause" (
	echo.
	pause
)

shift
if NOT "%1" == "" goto again

:EOF